package com.hero.tweaks.ui.fragments;

import net.margaritov.preference.colorpicker.ColorPickerPreference;
import android.os.Bundle;
import android.provider.Settings;

import com.hero.tweaks.R;
import com.hero.tweaks.util.AxisPreferenceFragment;
import com.hero.tweaks.util.CMDProcessor;
import com.hero.tweaks.util.Helpers;
import android.preference.Preference;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class App extends AxisPreferenceFragment implements
OnPreferenceChangeListener, Preference.OnPreferenceClickListener {
	
	private static final String PREF_SMS_SCREEN_ON = "pref_sms_screen_on";
	private static final String PREF_SMS_TO_MMS = "pref_sms_to_mms";
	private static final String PREF_NOISE_SUPPRESSION = "pref_noise_suppression";
	private static final String PREF_PHOTO_MASK = "pref_photo_mask";
	private static final String PREF_PHONE_INFO = "pref_phone_info";
	//private static final String PREF_VOLUME_WARNING = "pref_volume_warning";
	private static final String PREF_AUDIO_TUNING = "pref_audio_tuning";
	private static final String PREF_QUICK_UNLOCK = "pref_quick_unlock";
	private static final String PREF_LOCK_CARRIER_COLOR = "pref_lockscreen_carrier_color";
	
	public void Hreboot() {
		CMDProcessor.runSuCommand("setprop ctl.restart zygote");
	}
	
	SwitchPreference mSmsScreenOn;
	SwitchPreference mSmsToMMS;
	SwitchPreference mNoiseSuppression;
	SwitchPreference mPhotoMask;
	Preference mPhoneInfo;
	//SwitchPreference mVolumeWarning;
	SwitchPreference mAudioTuning;
	SwitchPreference mQuickUnlock;
	ColorPickerPreference mCarrierColor;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// Load the preferences from an XML resource
		addPreferencesFromResource(R.xml.prefs_app);
		
		mSmsScreenOn = (SwitchPreference) findPreference(PREF_SMS_SCREEN_ON);
		mSmsScreenOn.setOnPreferenceChangeListener(this);
		mSmsScreenOn.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_sms_screen_on", 0) == 1));
		
		mSmsToMMS = (SwitchPreference) findPreference(PREF_SMS_TO_MMS);
		mSmsToMMS.setOnPreferenceChangeListener(this);
		mSmsToMMS.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_sms_to_mms", 0) == 1));
		
		mNoiseSuppression = (SwitchPreference) findPreference(PREF_NOISE_SUPPRESSION);
		mNoiseSuppression.setOnPreferenceChangeListener(this);
		mNoiseSuppression.setChecked((Settings.System.getInt(
				getContentResolver(), "tweaks_disablenoise_suppression", 0) == 1));

		mPhoneInfo = findPreference(PREF_PHONE_INFO);
		mPhoneInfo.setOnPreferenceClickListener(this);

		/*mVolumeWarning = (SwitchPreference) findPreference(PREF_VOLUME_WARNING);
		mVolumeWarning.setOnPreferenceChangeListener(this);
		mVolumeWarning.setChecked(Settings.System.getInt(getContentResolver(),
				"audio_safe_volume_state", 0) == 1);*/
		
		mAudioTuning = (SwitchPreference) findPreference(PREF_AUDIO_TUNING);
		mAudioTuning.setOnPreferenceChangeListener(this);
		mAudioTuning.setChecked(Settings.System.getInt(getContentResolver(),
				"tweaks_volume_control", 0) == 1);
		
		mQuickUnlock = (SwitchPreference) findPreference(PREF_QUICK_UNLOCK);
		mQuickUnlock.setOnPreferenceChangeListener(this);
		mQuickUnlock.setChecked((Settings.System.getInt(getContentResolver(),
				"tweaks_fast_unlock", 0) == 1));
		
		mCarrierColor = (ColorPickerPreference) findPreference(PREF_LOCK_CARRIER_COLOR);
		mCarrierColor.setOnPreferenceChangeListener(this);
		
		mPhotoMask = (SwitchPreference) findPreference(PREF_PHOTO_MASK);
		mPhotoMask.setOnPreferenceChangeListener(this);
		mPhotoMask.setChecked((Settings.System.getInt(getContentResolver(),
				"tweaks_hide_photo_mask", 0) == 1));

	}

	@Override
	public boolean onPreferenceChange(Preference preference, Object objValue) {
		Cr = getActivity().getContentResolver();
		
		if (preference == mSmsScreenOn) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_sms_screen_on",
					value);
			return true;
			
		} else if (preference == mSmsToMMS) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_sms_to_mms",
					value);
			return true;
				
		} else if (preference == mNoiseSuppression) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(),
					"tweaks_disablenoise_suppression", value);
			setHasOptionsMenu(true);
			return true;
			
		/*} else if (preference == mVolumeWarning) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "audio_safe_volume_state",
					value);
			setHasOptionsMenu(true);
			return true;*/
			
		} else if (preference == mQuickUnlock) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(),
					"tweaks_fast_unlock", value);
			return true;
			
		} else if (preference == mCarrierColor) {
			String hex = ColorPickerPreference.convertToARGB(Integer
				.valueOf(String.valueOf(objValue)));
			preference.setSummary(hex);
	
			int intHex = ColorPickerPreference.convertToColorInt(hex);
			Settings.System.putInt(getContentResolver(),
				"tweaks_lockscreen_carrier_color", intHex);
			setHasOptionsMenu(true);
			return true; 
			
		} else if (preference == mAudioTuning) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_volume_control",
					value);
			setHasOptionsMenu(true);
			return true;
			
		} else if (preference == mPhotoMask) {
			boolean checked = ((SwitchPreference) preference).isChecked();
			int value = (checked ? 0 : 1);
			Settings.System.putInt(getContentResolver(), "tweaks_hide_photo_mask",
					value);
			setHasOptionsMenu(true);
			return true;
		}

		return false;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.layout.actionbar_actions, menu);
	}


	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case (R.id.restart_sysui):
			Helpers.restartHotreboot();
			return true;
		default:
			return super.onContextItemSelected(item);
		}
	}

	@Override
	public boolean onPreferenceClick(Preference preference) {

		return false;
	}

	@Override
	public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
			Preference preference) {

		return super.onPreferenceTreeClick(preferenceScreen, preference);
	}
}